package org.yoao.crm.tree;

import java.util.List;

import org.yoao.crm.business.impl.PremissionServiceImpl;
import org.yoao.crm.business.service.IPremissionService;
import org.yoao.crm.dao.MenuDAO;
import org.yoao.crm.struts.form.MenuForm;

/**
 * @author yxwonline.com
 * @description 构造树的XML
 * @date 2010-7-21 上午11:32:49 
 */
public class TreeBuilder {
	private IPremissionService treePremission = new PremissionServiceImpl();
	
	/**
	 * @author yxwonline.com
	 * @description 
	 * @param mode
	 * @param type
	 * @return
	 * @throws Exception
	 * @date 2010-7-22 上午09:48:51
	 */
	public String getTree(int mode, String userSno, String relations, String base) throws Exception{
		String sql = " from menu order by sortNumber asc";
		String[] fieldName = {"keyName", "title", "formType", "sortNumber", "layer", "isActive", "isFirstItem", "url", "accessSno" , "accessName", "target", "headImage"};
		return buildXML(MenuDAO.getMenuDAO().findAll(sql, fieldName), mode, userSno, relations, base);		
	}
	
	/**
	 * @author yxwonline.com
	 * @description 构建xml
	 * @param mode:1--左边显示的树(不显示隐藏菜单),0--右边列表框中显示的树(显示隐藏菜单)
	 * @return
	 * @throws Exception
	 * @date 2010-7-22 上午09:45:02
	 */
	private String buildXML(List data, int mode, String userSno, String relations, String base) throws Exception{
		StringBuffer result = new StringBuffer("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<MenuTree>\n");
		if(data == null || data.isEmpty()){
			System.out.println("从数据库中获取树为空!");
			result.append("</MenuTree>\n");
			return result.toString();
		}
		
		boolean isAdmin = true;
		if(1 == mode){
			isAdmin = treePremission.isAdmin(userSno);//是否管理员
		}
		
		
		//仅显示模式,删掉未激活的菜单
		if(1 == mode){
			for(int i=0;i<data.size();i++){
				MenuForm menuItem = (MenuForm)data.get(i);
				if(0 == menuItem.getIsActive()){
					data.remove(i);
					i--;
				}
			}
		}		
		
		MenuForm menuItem = null;
		boolean isFirst = true; //是否第一次
		boolean isParentHidden = false; //父节点是否为隐藏
		int currLayer = 0; //当前节点
		int pareLayer = 0; //上一节点
		int lessLayer = 0; //不处理的节点
		int hideLayer = 1000; //隐藏层
		Object object = null;		
		
		if(data != null){
			for(int i=0;i<data.size();i++){
				object = data.get(i);
				if(object != null){
					menuItem = (MenuForm)object;
					if(menuItem != null){
						boolean isCheck = true;
						if(1 == mode){
							if(isAdmin) //是管理员，不用检查权限
								isCheck = true;
							else //不是管理员，需要检查权限
								isCheck = treePremission.check(relations, menuItem.getAccessSno());
						}
						
						if(isCheck && menuItem.getLayer() <= hideLayer){
							hideLayer = 1000;
							currLayer = menuItem.getLayer();
							if(menuItem.getIsActive() == 0)
								isParentHidden = true;
							else if(menuItem.getIsActive() == 1)
								isParentHidden = false;
							
							if(isFirst){
								getStartNode(result, menuItem, base);
								isFirst = false;
							}else{
								if(currLayer > pareLayer){ //下一级
									getStartNode(result, menuItem, base);	
								}else if(currLayer == pareLayer){ //同一级
									getEndNode(result, menuItem);
									getStartNode(result, menuItem, base);
								}else if(currLayer < pareLayer){ //上一级
									for(int j=0;j<=(pareLayer-currLayer);j++){
										getEndNode(result, menuItem);
									}
									getStartNode(result, menuItem, base);
								}
							}
							pareLayer = currLayer;
						}else{
							lessLayer++;
							hideLayer = menuItem.getLayer();
						}
					}
				}
			}
			
			if(result.indexOf("<MenuNode ") > 0){
				for(int i=0;i<=pareLayer;i++){ 
					getEndNode(result, menuItem);
				}
			}
		}
		
		result.append("</MenuTree>\n");
		return result.toString();
	}
	
	private StringBuffer getStartNode(StringBuffer buffer, MenuForm menuItem, String base) {
		if(buffer != null) {
			if(menuItem.getAccessSno() == null) menuItem.setAccessSno("");
			buffer.append("<MenuNode name=\"").append(menuItem.getTitle())
			.append("\" id=\"").append(menuItem.getKeyName())
			.append("\" form=\"").append(menuItem.getFormType())
			.append("\" type=\"").append("")
			.append("\" index=\"").append(menuItem.getSortNumber())
			.append("\" layer=\"").append(menuItem.getLayer())
			.append("\" url=\"").append(menuItem.getUrl())
			.append("\" access=\"").append(menuItem.getAccessSno())
			.append("\" module=\"").append(base)
			.append("\" isActive=\"").append(menuItem.getIsActive())
			.append("\" isfirstitem=\"").append(menuItem.getIsFirstItem())
			.append("\" target=\"").append(menuItem.getTarget())
			.append("\" headImage=\"").append(menuItem.getHeadImage())
			.append("\">\n");
		}
		return buffer;
	}

	private StringBuffer getEndNode(StringBuffer buffer, MenuForm menuItem) {
		if(buffer != null) buffer.append("</MenuNode>\n");
		return buffer;
	}
}